//
//  VOH_SD_RegionalradioVC.m
//  VoiceOfHeart
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 吉龙. All rights reserved.
//
//区域电台UIView


//#import "VOH_RadioPlayerURLMdel.m"
#import "VOH_SD_RegionalradioVC.h"
#import "VOH_SD_RegionalCell.h"
#import "VoiceOfHeart.pch"
#import "VOH_SD_RegionalradioModel.h"

#import"VOH_SD_RegionalCell.h"
@interface VOH_SD_RegionalradioVC ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
    UIScrollView *_bigScrollview;
    
    NSMutableArray *array1;
    NSMutableArray *array2;
    NSMutableArray *array3;
    NSMutableArray *array4;
    
    
    UIView *_topView;
    UIView *_selectedView;
    UIButton *_selectedBtn;
    
}

@property(nonatomic,strong) NSMutableArray *areaArray;

@property(nonatomic, strong)UITableView *tableview;


@end

@implementation VOH_SD_RegionalradioVC
-(NSMutableArray *)areaArray{
    if (!_areaArray) {
        _areaArray = [NSMutableArray array];
        
    }
    return _areaArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"区域之声";
 
    

    array1 = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e", nil];
    array2 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    array3 = [NSMutableArray arrayWithObjects:@"-",@"=",@"+",@"/",@">", nil];
    array4 = [NSMutableArray arrayWithObjects:@"一",@"起",@"走",@"永",@"远", nil];
    [self initTopView];
    [self plistData];
    [self initUI];
    
//    UIButton *button = [UIButton ]
    for (UIButton *button in self.view.subviews) {
        if (button.tag == 1000) {
            [self turnMore:button];
        }
    }
    
    [self addTableView];
    
}
static NSString *cellidefierCell = @"Cells";

-(void)addTableView{
    self.tableview  = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_topView.frame), ScreenWidth, ScreenHeight - CGRectGetMaxY(_topView.frame))];
    [self.view addSubview:self.tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource =self;

    [self.tableview registerClass:[VOH_SD_RegionalCell class] forCellReuseIdentifier:cellidefierCell];
}

-(void)initTopView
{
    float topScrollHeight = 64;
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, topScrollHeight)];
    [_topView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_topView];
    NSArray *array = [[NSArray alloc]initWithObjects:@"本地电台",@"国家电台",@"省电台",@"网络台", nil];
    float width = WIDTH/array.count;
    _selectedView = [[UIView alloc]initWithFrame:CGRectMake(0, 64*2-3, width, 1)];
    _selectedView.backgroundColor = mainTone;
    [self.view addSubview:_selectedView];
    for (int i = 0; i<array.count; i++) {
        NSString *title = [array objectAtIndex:i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(width*i,0 , width, topScrollHeight);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:mainTone forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(topBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 1000+i;
        [_topView addSubview:btn];
        if (i==0) {
            _selectedBtn = btn;
            _selectedBtn.selected = YES;
        }
    }
    
}

-(void)initUI
{
    _bigScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64*2, WIDTH, HEIGHT)];
    _bigScrollview.contentSize = CGSizeMake(WIDTH *4, 0);
    _bigScrollview.backgroundColor = [UIColor grayColor];
    _bigScrollview.pagingEnabled = YES;
    _bigScrollview.bounces = NO;
    _bigScrollview.delegate = self;
    [self.view addSubview:_bigScrollview];
 
    
    for (int i = 0; i<4; i++) {
        UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH *i, 0, WIDTH ,_bigScrollview.frame.size.height)];
        [btn1 addTarget:self action:@selector(click1:) forControlEvents:(UIControlEventTouchUpInside)];
        btn1.tag = i+100;
        [_bigScrollview addSubview:btn1];
        
        
    }
}


-(void)click1:(UIButton *)sender{
    
    switch (sender.tag  ) {
        case 100:
            
            [self plistData] ;
            break;
        case 101:
            
            [self plistData] ;
            break;
        case 102:
            
            [self plistData] ;
            break;
        case 103:
            
            [self plistData] ;
            break;
        
        default:
            break;
    }
    
    
    
    
    
    
}


//解析

-(void)plistData{
  NSString *urlStr =@"http://live.ximalaya.com/live-web/v1/getRadiosListByType?device=iPhone&pageNum=1&pageSize=30&provinceCode=110000&radioType=2";
    NSURL *url = [NSURL URLWithString:urlStr];
    //
    NSMutableURLRequest *reqest = [NSMutableURLRequest requestWithURL:url];
    //
    [NSURLConnection sendAsynchronousRequest:reqest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        if ([dic[@"total"] integerValue]) {
            for (NSDictionary *dict2 in dic[@"result"]) {
                VOH_SD_RegionalradioModel *mo =[VOH_SD_RegionalradioModel new ];
                [mo setValuesForKeysWithDictionary:dict2];
                [self.areaArray addObject:mo];
            }
        }
        [self.tableview reloadData];
    }];

}





- (void)turnMore:(UIButton *)button{
    
    switch (button.tag) {
        case 100:{

            NSString *blockMessage = @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?device=iPhone&pageNum=1&pageSize=30&provinceCode=110000&radioType=2";

//            self.block(blockMessage);
        }
            break;
            
        case 1001:{
//            self.block(@"国家台,http://live.ximalaya.com/live-web/v1/getRadiosListByType?device=iPhone&pageNum=1&pageSize=30&provinceId=%28null%29&radioType=1");
//            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//            NSString *cityID = [userDefaults objectForKey:@"CityID"];
//            NSLog(@"%@", cityID);
                   }
            break;
//        case 1002:
//            self.block(@"省市台,http://live.ximalaya.com/live-web/v1/getProvinceList?device=iPhone,http://live.ximalaya.com/live-web/v1/getRadiosListByType?device=iPhone&pageNum=1&pageSize=30&provinceCode=110000&radioType=2");
//            break;
//        case 1003:
//            self.block(@"网络台,http://live.ximalaya.com/live-web/v1/getRadiosListByType?device=iPhone&pageNum=1&pageSize=30&provinceId=%28null%29&radioType=3,http://live.ximalaya.com/live-web/v1/getRadiosListByType?device=iPhone&pageNum=2&pageSize=30&provinceId=%28null%29&radioType=3");
//            break;
//            
//        case 1004:
//            self.block(@"电台排行榜,http://live.ximalaya.com/live-web/v1/getTopRadiosList?device=iPhone&radioNum=100");
//            break;
//            
        default:
            break;
    }
}

#pragma  mark--scrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_bigScrollview]) {
        int contentoffset = scrollView.contentOffset.x;
        int numOfTable = contentoffset/WIDTH;
        UIButton *btn = (UIButton *)[self.view viewWithTag:100+numOfTable];
        [self topBtnClick:btn];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y<0) {
        return;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView.contentOffset.y<0) {
        return;
    }
}

-(void)topBtnClick:(UIButton *)sender
{
    if (_selectedBtn == sender) {
        return;
    }
    _selectedBtn.selected = NO;
    _selectedBtn = sender;
    _selectedBtn.selected = YES;
    
    float width = WIDTH/4;
    [_bigScrollview setContentOffset:CGPointMake(WIDTH*(sender.tag-100), 0) animated:YES];
    [UIView animateWithDuration:0.3 animations:^{
        _selectedView.frame = CGRectMake(width*(sender.tag-100), 64*2-3, width, 1);
    }];
    [self turnMore:sender];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.areaArray.count;
//    switch (tableView.tag-200) {
//        case 0:
//        {
//            return array1.count;
//        }
//            break;
//        case 1:
//        {
//            return array2.count;
//        }
//            break;
//        case 2:
//        {
//            return array3.count;
//        }
//            break;
//        case 3:
//        {
//            return array4.count;
//        }
//            break;
//        default:
//            return 0;
//            break;
//    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    VOH_SD_RegionalCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidefierCell forIndexPath:indexPath];
    
    VOH_SD_RegionalradioModel *regionalMdel = self.areaArray[indexPath.row];
    
    
    cell.radioNameLB.text = regionalMdel.rname;
    cell.programName.text = [NSString stringWithFormat: @"直播中:%@",regionalMdel.programName];
    cell.personNumber.text =[NSString stringWithFormat:@"收听人数:%ld", regionalMdel.radioPlayCount];
 
    
    return cell;
    
}
   
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}


@end
